﻿using MCPlatform.Communicate.Modbus;
using MCPlatform.Communicate.Modbus.Modbus;
using MCPlatform.Communicate.TcpIp;
using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MCPlatform.UI.Controls.Views.Communicate
{
    /// <summary>
    /// TcpClientControl.xaml 的交互逻辑
    /// </summary>
    public partial class TcpClientControl : UserControl
    {
        public TcpClientControl()
        {
            InitializeComponent();
        }

        #region Properties





        public TcpClientModel TcpClientModel
        {
            get { return (TcpClientModel)GetValue(TcpClientModelProperty); }
            set { SetValue(TcpClientModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TcpClientModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TcpClientModelProperty =
            DependencyProperty.Register("TcpClientModel", typeof(TcpClientModel), typeof(TcpClientControl), new PropertyMetadata(default));




        public NTcpClient? NTcpClient { get; set; }
       




        #endregion




        #region Methods
        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {
            String path = Paths.TcpClientConfigPaths;
            SaveConfigData.SaveConfig<TcpClientModel>(path, this.TcpClientModel);
        }

        private void btn_Debug_Click(object sender, RoutedEventArgs e)
        {
            if (this.NTcpClient is null || !this.NTcpClient.Connected)
            {
                Toast.Show("TCP服务器未连接！", targetWindow: ToastWindow.MainWindow);
                return;
            }
            new TcpClientDebugView(this.NTcpClient).Show();
        }

        private void btn_Link_Click(object sender, RoutedEventArgs e)
        {
            if (NTcpClient is null)
            {
                Toast.Show("TCP Client 为空！", targetWindow: ToastWindow.MainWindow);
                return;
            }


            if (NTcpClient.IsConnected)
            {
                NTcpClient.Disconnect();
            }
            else
            {
                 var res = NTcpClient.Connect();
                if (!res)
                {
                    Toast.Show("客户端连接失败！",targetWindow:ToastWindow.MainWindow);
                }
            }
            SetUIDisable(NTcpClient.IsConnected);
        }

        private void SetUIDisable(bool enable)
        {
            //tb_IP.IsEnabled = enable;
            //tb_Port.IsEnabled = enable;      
            //tb_ClientName.IsEnabled = enable;
            //sw_AutoRetry.IsEnabled = enable;
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            if (NTcpClient is null)
            {
                // NTcpClient = new NTcpClient(this.TcpClientModel);
                NTcpClient = this.TcpClientModel.NTcpClient;
            }
        }

        #endregion
    }
}
